package com.dcy.daily.skill.rabbitMQ.springAMQP.tmp.amqpTemplate;

import com.dcy.daily.common.config.annotation.ComponentUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;

import java.util.ArrayList;
import java.util.List;

/**
 * 使用AmqpTemplate来发送消息
 * @author duanchunyu
 * @version 1.0.0
 * @createTime 2021年07月22日 11:12
 */
@ComponentUtil.CComponent
public class Send implements ApplicationRunner {

    public static final Logger logger = LoggerFactory.getLogger(Send.class);

    @Autowired
    private RabbitTemplate rabbitTemplate;


    /**
     *
     * @param args
     * @throws Exception
     */
    @Override
    public void run(ApplicationArguments args) throws Exception {
        rabbitTemplate.setMandatory(true);
        rabbitTemplate.convertAndSend("myqueue","test");

        rabbitTemplate.setReturnCallback((message, replyCode, replyText, exchange, routingKey) ->{
            logger.info(new String(message.getBody()));
            System.out.println(message);
        });

        rabbitTemplate.setBeforePublishPostProcessors();
        rabbitTemplate.setBeforePublishPostProcessors();
        rabbitTemplate.setConfirmCallback((correlationData, ack,cause) -> {
            logger.info(correlationData +"Ack: " + ack + ":" + ack);
        });
    }

    /**
     * 要发送的消息
     * @return
     */
    public List getMessagesToSend(){
        ArrayList<String> messages = new ArrayList<>();
        messages.add("1");
        messages.add("2");
        messages.add("3");
        messages.add("4");
        messages.add("5");
        return messages;
    }
}
